package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.model.po.Dept;
import com.example.demo.model.po.WorkOrder;
import com.example.demo.model.vo.WorkOrderVo;
import com.example.demo.service.DeptService;
import com.example.demo.service.WorkOrderService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/post/order")
public class WorkOrderController {

    @Resource
    private WorkOrderService workOrderService;
    @Resource
    private DeptService deptService;


    //添加
    @PostMapping("/save")
    public boolean addOrder(@RequestBody WorkOrder workOrder) {
        if(workOrder.getOrderNo()==null&&workOrder.getOrderType()==null
                &&workOrder.getTitle().equals("")&&workOrder.getContent().equals("")){
            throw new RuntimeException("必填项没有填写完整，请检查。");
        }
        return workOrderService.save(workOrder);
    }

    // 根据id删除
    @GetMapping("/delete/{id}")
    public boolean deleteOrder(@PathVariable Long id) {
        return workOrderService.removeById(id);
    }

    // 修改信息
    @PostMapping("/update")
    public boolean updateOrder(@RequestBody WorkOrder workOrder) {
        if(workOrder.getOrderNo()==null&&workOrder.getOrderType()==null
                &&workOrder.getTitle().equals("")&&workOrder.getContent().equals("")&&workOrder.getCreateTime()!=-1){
            throw new RuntimeException("必填项没有填写完整，请检查。");
        }
        return workOrderService.updateById(workOrder);
    }


    // 分页查询
    @GetMapping("/search")
    public Page<WorkOrder> pageOrders(@RequestParam int current, @RequestParam int size) {
        Page<WorkOrder> page = new Page<>(current, size);
        QueryWrapper<WorkOrder> queryWrapper = new QueryWrapper<>();
//        queryWrapper.orderByDesc("id");
        return workOrderService.page(page, queryWrapper);
    }

    //分派
    @GetMapping("/fenpai")
    public boolean addFenPai(@RequestParam Long id, @RequestParam Integer deptId, @RequestParam String deptName) {
        List<Dept> deptList = deptService.queryAll(deptId,deptName);
        if (deptList.size()==0){
            throw new RuntimeException("处理部门id无效，请检查。");
        }
        WorkOrder workOrder = workOrderService.getById(id);
        workOrder.setHandleDeptId(deptId);
        workOrder.setFenpaiTime(System.currentTimeMillis());
        return workOrderService.updateByFenpai(workOrder);
    }
    /*
   超期率=超期工单总量/工单总量
    4.查询7月每天的工单总量、超期率 （三选二）
    5.查询7月每个部门的工单总量、超期率。（三选二）List<WorkOrderVo> getDepartmentStatsForJuly()
    6.查询7月每个工单类型的工单总量、超期率。（三选二）List<WorkOrderVo> selectOrderTypeStatsForJuly()
     */
    @GetMapping("/getDepartmentStatsForJuly")
    public List<WorkOrderVo> getDepartmentStatsForJuly(){//查询7月每个部门的工单总量、超期率
        return workOrderService.getDepartmentStatsForJuly();
    }

    @GetMapping("/getOrderTypeStatsForJuly")
    public List<WorkOrderVo> getOrderTypeStatsForJuly(){//查询7月每个工单类型的工单总量、超期率
        return workOrderService.getOrderTypeStatsForJuly();
    }

}
